package com.hanrui.android.net.presenter;

import android.content.Context;
import android.util.Log;

import com.hanrui.android.activity.LoginActivity;
import com.hanrui.android.library.base.BaseBean;
import com.hanrui.android.library.base.BasePresenter;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.model.HistoricFlowBean;
import com.hanrui.android.library.net.ApiService;
import com.hanrui.android.library.net.RetrofitManager;
import com.hanrui.android.library.net.RxSchedulers;
import com.hanrui.android.net.contract.LcContract;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * 流程图
 * Created by Administrator on 2018/3/12.
 */

public class LcPresenter extends BasePresenter<LcContract.View> implements LcContract.Presenter {
    @Inject
    public LcPresenter(){
    }


    @Override
    public void loadData(final Context context, String procInsId, String bizId) {
        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .sealusehistoicFlow(procInsId,bizId)
                .compose(RxSchedulers.<BaseBean<HistoricFlowBean>>applySchedulers())
                .compose(mView.<BaseBean<HistoricFlowBean>>bindToLife())
                .subscribe(new Consumer<BaseBean<HistoricFlowBean>>() {
                    @Override
                    public void accept(BaseBean<HistoricFlowBean> dataResponse) throws Exception {
                        Log.e(Constant.tag,dataResponse.getCode()+"");
                        if (dataResponse.getCode() == 1){
                            mView.loadData(dataResponse.getData().getHistoicFlowList());
                        }else if (dataResponse.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else {
                            mView.showFaild(String.valueOf(dataResponse.getMessage()));
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild(throwable.getMessage());
                    }
                });
    }


}
